package it.asvp.asvp_portal.data.dao.impl;


import it.asvp.asvp_portal.data.dao.ArtDao;
import it.asvp.asvp_portal.data.entity.art.Art;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class ArtDaoImpl implements ArtDao{

	private static final Log log = LogFactory.getLog(ArtDaoImpl.class);

	@Autowired
	SessionFactory sessionFactory;

	public Long persist(Art transientInstance) {
		log.debug("persisting Art instance");
		Long id;
		try {
			id=(Long) sessionFactory.getCurrentSession().save(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
		return id;
	}

	public void attachDirty(Art instance) {
		log.debug("attaching dirty Art instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}


	public void delete(Art persistentInstance) {
		log.debug("deleting Art instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Art merge(Art detachedInstance) {
		log.debug("merging Art instance");
		try {
			Art result = (Art) sessionFactory.getCurrentSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Art findById(java.lang.Long id) {
		log.debug("getting Art instance with id: " + id);
		try {
			Art instance = (Art) sessionFactory.getCurrentSession().get(
					"it.asvp.asvp_portal.data.dao.generator.Art", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Art> findAll() {
		log.debug("getting all Art instances:");
	
			Criteria cr=sessionFactory.getCurrentSession().createCriteria(Art.class);
			List<Art> list= (List<Art>)cr.list();
			return list;
		}
}
